from django.shortcuts import redirect
from django.utils.deprecation import MiddlewareMixin


class ConfirmLoginMiddleware(MiddlewareMixin):

    def process_request(self, request):
        path = request.path
        allow_path = [
            '/', '/train/upload/', '/train/execute_predict/', '/train/download/',
            '/predict/upload/', '/predict/execute_predict/', '/predict/download/',
            '/train/execute_train/', '/predict/execute_train/', '/predict/select_mode/',
            '/train/confirm/',
        ]
        print(f'path = {path}')

        # 当有人登录就想访问的时候，跳转登录界面
        if path not in allow_path:
            return redirect('home')

    def process_response(self, request, response):
        return response